package com.ethan.crm.web.controller;

import com.ethan.crm.domain.Employee;
import com.ethan.crm.domain.Menu;
import com.ethan.crm.page.PageResult;
import com.ethan.crm.query.EmployeeQueryObject;
import com.ethan.crm.service.IEmployeeService;
import com.ethan.crm.service.IMenuService;
import com.ethan.crm.service.IPermissionService;
import com.ethan.crm.util.AjaxResult;
import com.ethan.crm.util.PermissionsUtil;
import com.ethan.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private IMenuService menuService;

    @RequestMapping("/employee")
    public String index(){
        return "employee";
    }

    /**
     * 查询员工列表信息
     * @param qo
     * @return
     */
    @RequestMapping("/employee_list")
    @ResponseBody
    public PageResult getEmployeeList(EmployeeQueryObject qo){
        return employeeService.queryForPage(qo);
    }

    /**
     * 新增保存员工的信息
     * @param employee
     * @return
     */
    @RequestMapping("/employee_save")
    @ResponseBody
    public AjaxResult saveEmployee(Employee employee){
        return employeeService.insert(employee);
    }

    /**
     * 更新员工的信息
     * @param employee
     * @return
     */
    @RequestMapping("/employee_update")
    @ResponseBody
    public AjaxResult updateEmployee(Employee employee){
        AjaxResult result ;
        try {
            employeeService.updateByPrimaryKey(employee);
            result = new AjaxResult(true,"更新员工信息成功！");
        } catch (Exception e) {
            result = new AjaxResult(true,"更新员工信息失败，请联系管理员！");
        }
        return result;
    }


    @RequestMapping("/employee_delete")
    @ResponseBody
    public AjaxResult deleteEmployee(Long id){
        AjaxResult result;
        try {
            employeeService.updateState(id);
            result = new AjaxResult(true,"删除员工信息成功！");
        } catch (Exception e) {
            result = new AjaxResult(true,"删除员工信息失败，请联系管理员！");
        }
        return result;
    }


    /**
     * 登录页面
     * 登录页面其实做了很多操作，好多应用就是初次登录会慢好多
     * @param username  用户名
     * @param password  密码
     * @param request   请求
     * @return          返回值
     */
    @RequestMapping("/login")
    @ResponseBody
    public AjaxResult login(String username, String password, HttpServletRequest request){
        //登录页因为不经过过滤器，所以在此将request对象放到ThreadLocal中
        UserContext.set(request);
        AjaxResult result;
        Employee currentUser = employeeService.getEmployeeForLogin(username, password);
        if (null != currentUser){
            request.getSession().setAttribute(UserContext.USER_IN_SESSION,currentUser);
            //根据用户id查询该用户的所有权限放到session中
            List<String> userPermissions = permissionService.queryByEid(currentUser.getId());
            request.getSession().setAttribute(UserContext.PERMISSION_IN_SESSION,userPermissions);
            //将该用户的菜单权限保存到session中
            List<Menu> allRootMenu = menuService.queryForRoot();
            PermissionsUtil.checkMenu(allRootMenu);
            request.getSession().setAttribute(UserContext.MENU_IN_SESSION,allRootMenu);
            result = new AjaxResult(true,"登录成功!");
        }else {
            result = new AjaxResult("账号密码有误!");
        }
        return result;
    }
}
